<?php
	
	function dbconnect() 
	{ 
		require('config.php'); 
		static $connect = null; 
		if ($connect === null) 
			{
				 $connect =  mysql_connect($host, $user, $passwd) or die(mysql_error());
				 mysql_query("SET character_set_results=utf8", $connect);
				 mysql_select_db($database); 
			} 
		return $connect; 
	} 
	
	function insert_user($post) 
	{ 
		
		$result= mysql_query("INSERT INTO nguoi_dung SET Ten_dang_nhap=N'".$post['Ten_dang_nhap']."', Ho_va_ten=N'".$post['Ho_va_ten']."', Gioi_tinh=N'".$post['Gioi_tinh']."', Mat_khau=N'".$post['Mat_khau']."', Email=N'".$post['Email']."', Dien_thoai=N'".$post['Dien_thoai']."', Ngay_sinh=N'".$post['Ngay_sinh']."', Ngay_dang_ky=NOW(), ID_Tinh_thanh_pho=N'".$post['ID_Tinh_thanh_pho']."', ID_Quan_huyen=N'".$post['ID_Quan_huyen']."', Dia_chi_chi_tiet=N'".$post['Dia_chi_chi_tiet']."', ID_Nhom_nguoi_dung='2' ", dbconnect()); 
		if ($result)
			{
		echo "<script> alert('Dữ liệu đã thêm thành công!')</script>";
		 header('Location: dangky.php');
		mysql_close();

			}
			else
			{
			echo "<script> alert('Quá trình thêm dữ liệu bị lỗi!')</script>" . mysql_error();
			}
					
	}
	
	function insert_user_inadminsite($post) 
	{ 
		
		$result= mysql_query("INSERT INTO nguoi_dung SET Ten_dang_nhap=N'".$post['Ten_dang_nhap']."', Ho_va_ten=N'".$post['Ho_va_ten']."', Gioi_tinh=N'".$post['Gioi_tinh']."', Mat_khau=N'".$post['Mat_khau']."', Email=N'".$post['Email']."', Dien_thoai=N'".$post['Dien_thoai']."', Ngay_sinh=N'".$post['Ngay_sinh']."', Ngay_dang_ky=NOW(), ID_Tinh_thanh_pho=N'".$post['ID_Tinh_thanh_pho']."', ID_Quan_huyen=N'".$post['ID_Quan_huyen']."', Dia_chi_chi_tiet=N'".$post['Dia_chi_chi_tiet']."', ID_Nhom_nguoi_dung=N'".$post['ID_Nhom_nguoi_dung']."' ", dbconnect()); 
		if ($result)
			{
		echo "<script> alert('Dữ liệu đã thêm thành công!')</script>";
		
		mysql_close();

			}
			else
			{
			echo "<script> alert('Quá trình thêm dữ liệu bị lỗi!')</script>" . mysql_error();
			}
					
	}
	function getid_auto_increment()
	{
		
		$r= mysql_query("SHOW TABLE STATUS LIKE 'Nguoi_dung'",dbconnect());
		$row = mysql_fetch_array($r); 
        $n = $row['Auto_increment']; 
        return $n;
		
		} 
	function getbyall_t()
	{ 
		$allnxb_req = mysql_query("SELECT * FROM tinh_thanh_pho" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
	
	
		function check_login($login) 
	{ 
	 
		 $result = mysql_query("select * from Nguoi_dung where Ten_dang_nhap=N'".$login['Ten_dang_nhap']."' and  Mat_khau=N'".$login['Mat_khau']."'",dbconnect());
		 $count=mysql_num_rows($result); 
		  if($count==1)
		  	{
			   session_start();
		       $row=mysql_fetch_array($result);
			   $_SESSION['ID_Nguoi_dung'] = $row['ID'];
			   $_SESSION['Ten_dang_nhap'] = $row['Ten_dang_nhap'];
			   $_SESSION['Mat_khau'] = $row['Mat_khau'];
			   $_SESSION['Ho_va_ten'] = $row['Ho_va_ten'];
			   $_SESSION['Email'] = $row['Email'];
			   $_SESSION['ID_Nhom_nguoi_dung'] = $row['ID_Nhom_nguoi_dung'];
			   
			header('Location: index.php');
			   
			}
			else
			{
				
				echo "<script> alert('Đăng nhập thất bại');</script>" ;
			}
	
	} 

	function getbyid_nhomnguoidung($id)
	{
		$result= mysql_query("SELECT * FROM Nhom_nguoi_dung WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyid_quanhuyen($id)
	{
		$result= mysql_query("SELECT * FROM quan_huyen WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}

	function getbyall_nhomnguoidung()
			{ 
				$allnxb_req = mysql_query("SELECT * FROM nhom_nguoi_dung" ,dbconnect()); 
				$result = array(); 
				while ($nxb = mysql_fetch_array($allnxb_req))
				 { 
					$result[] = $nxb; 
				 } 
				return $result; 
			} 
	
	function getbyid_nguoidung($id)
			{
				$result= mysql_query("SELECT * FROM Nguoi_dung WHERE ID=N'".$id."'",dbconnect());
				return mysql_fetch_array($result); 
			}
			
			
	function update_nguoidung($bv)
	{
	$query= mysql_query("UPDATE Nguoi_dung SET 
	Ten_dang_nhap=N'".$bv['Ten_dang_nhap']."', 
	Ho_va_ten = N'".$bv['Ho_va_ten']."', 
	Mat_khau = N'".$bv['Mat_khau']."',
	Gioi_tinh = N'".$bv['Gioi_tinh']."',
	Dien_thoai = N'".$bv['Dien_thoai']."',
	Ngay_sinh = N'".$bv['Ngay_sinh']."',
	Email = N'".$bv['Email']."',
	ID_Tinh_thanh_pho = N'".$bv['ID_Tinh_thanh_pho']."',
	ID_Quan_huyen = N'".$bv['ID_Quan_huyen']."',
	Dia_chi_chi_tiet = N'".$bv['Dia_chi_chi_tiet']."',
	ID_Nhom_nguoi_dung = N'".$bv['ID_Nhom_nguoi_dung']."'
	WHERE ID=N'".$bv['ID']."' ", dbconnect()); 	
	}
	
	function update_nguoidung2($bv)
	{
	$query= mysql_query("UPDATE Nguoi_dung SET 
	Ten_dang_nhap=N'".$bv['Ten_dang_nhap']."', 
	Ho_va_ten = N'".$bv['Ho_va_ten']."', 
	Mat_khau = N'".$bv['Mat_khau']."',
	Gioi_tinh = N'".$bv['Gioi_tinh']."',
	Dien_thoai = N'".$bv['Dien_thoai']."',
	Ngay_sinh = N'".$bv['Ngay_sinh']."',
	Email = N'".$bv['Email']."',
	ID_Tinh_thanh_pho = N'".$bv['ID_Tinh_thanh_pho']."',
	ID_Quan_huyen = N'".$bv['ID_Quan_huyen']."',
	Dia_chi_chi_tiet = N'".$bv['Dia_chi_chi_tiet']."',
	ID_Nhom_nguoi_dung = '2'
	WHERE ID=N'".$bv['ID']."' ", dbconnect()); 	
	}	
	function update_nguoidung3($bv)
	{
	$query= mysql_query("UPDATE Nguoi_dung SET 
	Ten_dang_nhap=N'".$bv['Ten_dang_nhap']."', 
	Ho_va_ten = N'".$bv['Ho_va_ten']."', 
	Gioi_tinh = N'".$bv['Gioi_tinh']."',
	Dien_thoai = N'".$bv['Dien_thoai']."',
	Ngay_sinh = N'".$bv['Ngay_sinh']."',
	Email = N'".$bv['Email']."',
	ID_Tinh_thanh_pho = N'".$bv['ID_Tinh_thanh_pho']."',
	ID_Quan_huyen = N'".$bv['ID_Quan_huyen']."',
	Dia_chi_chi_tiet = N'".$bv['Dia_chi_chi_tiet']."',
	ID_Nhom_nguoi_dung = '2'
	WHERE ID=N'".$bv['ID']."' ", dbconnect()); 	
	}	
?>